﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YArchitech.Revit;

namespace HYFloorPipeModule
{
	public class InitialCurveNode
	{
		public InitialCurveNode(IList<ModelLine> listMLine)
		{
			this.modeLines = listMLine;
		}

		public void StructureNodes()
		{
			CurveElementNode curveElementNode = null;
			foreach (ModelLine modelLine in this.modeLines)
			{
				this.StructureNode(ref curveElementNode, modelLine);
			}
			this.bottomNode = curveElementNode;
		}

		public CurveElementNode GetRootNode()
		{
			this.rootNode = this.bottomNode;
			while (this.rootNode.PreView != null)
			{
				this.rootNode = this.GetPreViewNode(this.rootNode);
			}
			return this.rootNode;
		}

		public CurveElementNode GetBottomNode()
		{
			return this.bottomNode;
		}

		private CurveElementNode GetPreViewNode(CurveElementNode node)
		{
			if (node.PreView == null)
			{
				return node;
			}
			return node.PreView;
		}

		private void StructureNode(ref CurveElementNode curveNode, ModelLine modelLine)
		{
			CurveElementNode curveElementNode = curveNode;
			if (curveNode == null)
			{
				curveElementNode = new CurveElementNode(modelLine, this.GetCurve(modelLine).GetEndPoint(0));
				curveNode = curveElementNode;
				return;
			}
			CurveElementNode curveElementNode2 = new CurveElementNode(modelLine, this.GetCurve(modelLine).GetEndPoint(0));
			curveElementNode.Next = curveElementNode2;
			curveElementNode2.PreView = curveElementNode;
			curveNode = curveElementNode.Next;
		}

		private Curve GetCurve(CurveElement curveElem)
		{
			return (curveElem.Location as LocationCurve).Curve;
		}

		private IList<ModelLine> modeLines;

		private CurveElementNode rootNode;

		private CurveElementNode bottomNode;
	}
}
